package com.cl.it.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.util.Date;

public class DateUtils {

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String dateConvert(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        content = content.trim();
        if (content.indexOf("天前") != -1) {
            int days = 0 - Integer.valueOf(content.replaceAll("天前", "").trim());
            return format(addDays(new Date(), days), "yyyy-MM-dd");
        } else if (content.matches("^\\d{1,2}小时\\d{1,2}分钟前")) {
            int hours = Integer.valueOf(content.substring(0, content.indexOf("小时")));
            int mins = Integer.valueOf(content.substring(content.indexOf("小时") + 2, content.indexOf("分钟前")));
            int totalMins = -(hours * 60 + mins);
            return format(addMinutes(new Date(), totalMins), "yyyy-MM-dd");
        }else if (content.indexOf("小时前") != -1) {
            int hours = 0 - Integer.valueOf(content.replaceAll("小时前", "").trim());
            return format(addHours(new Date(), hours), "yyyy-MM-dd");
        } else if (content.matches("^\\d{1,2}月\\d{1,2}日$")) {
            String yyyy = DateFormatUtils.format(new Date(), "yyyy");
            content = yyyy + "年" + content;
            try {
                return format(org.apache.commons.lang3.time.DateUtils.parseDate(content, "yyyy年MM月dd日"), "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (content.matches("^\\d{1,2}\\-\\d{1,2}$")) {
            String yyyy = DateFormatUtils.format(new Date(), "yyyy");
            content = yyyy + "-" + content;
            return content;
        } else {
            try {
                Date d = org.apache.commons.lang3.time.DateUtils.parseDate(content,
                        "yyyy年MM月dd日", "yyyy年MM月dd日 HH:mm", "yyyy-MM-dd",
                        "yyyy-MM-dd HH:mm", "yyyy/MM/dd HH:mm", "yyyy年MM月dd日 HH:mm:ss");
                return format(d, "yyyy-MM-dd");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return content;
    }


    public static String format(Date date) {
        return format(date, DEFAULT_PATTERN);
    }

    public static String format(Long millis) {
        return format(millis, DEFAULT_PATTERN);
    }

    public static String format(Date date, String pattern) {
        return date != null ? DateFormatUtils.format(date, pattern) : null;
    }

    public static String format(Long millis, String pattern) {
        return millis != null ? DateFormatUtils.format(millis, pattern) : null;
    }

    public static Date parse(String date) throws ParseException{
        return date != null ? org.apache.commons.lang3.time.DateUtils.parseDate(date, DEFAULT_PATTERN) : null;
    }

    public static Date parse(String date,String pattern) throws ParseException{
        return date != null ? org.apache.commons.lang3.time.DateUtils.parseDate(date, pattern) : null;
    }

    public static Date parse(Long millis){
        return new Date(millis);
    }

    public static Date now(){
        return new Date();
    }

    public static Date addYears(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addYears(date, amount);
    }

    public static Date addMonths(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addMonths(date, amount);
    }

    public static Date addDays(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addDays(date, amount);
    }

    public static Date addHours(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addHours(date, amount);
    }

    public static Date addMinutes(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addMinutes(date, amount);
    }

    public static Date addSeconds(Date date,int amount){
        return org.apache.commons.lang3.time.DateUtils.addSeconds(date, amount);
    }
}
